package com.hmily.dubbo.provider.sync;

import com.hmily.dubbo.api.UserUpdateFacade;
import com.hmily.dubbo.provider.impl.NewUserUpdateFacadeImpl;
import com.hmily.dubbo.provider.impl.UserUpdateFacadeImpl;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <h1>利用 java 代码来启动服务。</h1>
 *
 * @author hmilyylimh
 *         ^_^
 * @version 0.0.1
 *         ^_^
 * @date 2021-10-19
 *
 */
public class ThreeCfgVersionProviderApplication {

    public static void main(String[] args) throws IOException {
        // 设置应用服务名称
        ApplicationConfig applicationConfig = new ApplicationConfig("dubbo-provider");
        // 设置注册中心的地址
        RegistryConfig registryConfig = new RegistryConfig("zookeeper://127.0.0.1:2181");

        // 创建暴露 UserUpdateFacade 这个服务的 1.0.0 旧版本，注意 ref 的值
        ServiceConfig<UserUpdateFacade> serviceConfig = new ServiceConfig<>();
        serviceConfig.setApplication(applicationConfig);
        serviceConfig.setRegistry(registryConfig);
        serviceConfig.setInterface(UserUpdateFacade.class);
        serviceConfig.setRef(new UserUpdateFacadeImpl());
        serviceConfig.setVersion("1.0.0");
        serviceConfig.export();

        // 创建暴露 UserUpdateFacade 这个服务的 2.0.0 新版本，注意 ref 的值
        ServiceConfig<UserUpdateFacade> serviceConfig2 = new ServiceConfig<>();
        serviceConfig2.setApplication(applicationConfig);
        serviceConfig2.setRegistry(registryConfig);
        serviceConfig2.setInterface(UserUpdateFacade.class);
        serviceConfig2.setRef(new NewUserUpdateFacadeImpl());
        serviceConfig2.setVersion("2.0.0");
        serviceConfig2.export();

        System.out.println("Dubbo " + UserUpdateFacade.class.getSimpleName() + " started!");
        System.out.println(new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss]").format(new Date()) + " Dubbo service server started!");

        System.in.read();
    }
}
